package com.example.shardings.tb.service;

import com.alibaba.fastjson.JSONArray;
import com.example.shardings.tb.mapper.WechatUserMapper;
import com.example.shardings.tb.pojo.WechatUser;
//import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class WechatUserService {

    @Autowired
    private WechatUserMapper wechatUserMapper;

    public List<WechatUser> getAll() {
        //PageHelper.startPage(1,10);
       // PageHelper.orderBy(" sort asc,id asc  ");
        WechatUser s=new WechatUser();
        return  wechatUserMapper.select(s);
    }

    public List<WechatUser> getById(Long id) {
        //PageHelper.startPage(1,30);
        //PageHelper.orderBy(" sort asc,id asc  ");
        WechatUser s=new WechatUser();
        s.setId(id);
        return  wechatUserMapper.select(s);
    }

    public    List<WechatUser>  initDate() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("D:\\git\\shardings\\init_data.json"));
        BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
        StringBuilder dataJson=new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            dataJson.append(line);
        }
        List<WechatUser> wxs = JSONArray.parseArray(dataJson.toString(),WechatUser.class);
        List<WechatUser> rs=new ArrayList<>();
        for (WechatUser wechatUser : wxs) {
            Random rd=new Random();
            wechatUser.setSort(rd.nextInt(wxs.size()*100));
            wechatUserMapper.insertSelective(wechatUser);
            rs.add(wechatUser);
        }
        return rs;
    }

    public static void main(String[] args) {
        String s=null;
        System.out.println((s!=null)&0>0);
    }
}
